<?php

class ReleasesWidget extends WP_Widget {

	function __construct(){
		$widget_ops = array('classname'		=>	'widget_releases',
							'description'	=>	__('Перечень игровых релизов'));
		$this->WP_Widget('ReleasesWidget', __('КА | Игровые релизы'), $widget_ops);
	}

	public function form($instance){
		$instance = wp_parse_args((array) $instance, array(	'count'	=> 10 ));
		?>
			<label for='<?php echo $this->get_field_id('count'); ?>'><?php _e('Количество релизов'); ?></label> 
			<input style="float:right; width: 100%" id='<?php echo $this->get_field_id('title'); ?>' name='<?php echo $this->get_field_name('count'); ?>' type='text' value="<?php echo attribute_escape($instance['count']); ?>"/>
			<br/>
		<?php
	}

	public function update($new_instance, $old_instance){
		$instance = $old_instance;
		$instance['count']	=	$new_instance['count'];
		return $instance;
	}
	
	public function widget($args, $instance){
		extract($args, EXTR_SKIP);
		echo $before_widget;
		?>
		<div class="relises-widget widget">
			<div class="controll">
				<div class="new-in-sale relises-tab relises-active">
					Новые в продаже
				</div>
				<div class="future relises-tab">
					Скоро выходят
				</div>
			</div>
			<div class="relises-display">
				<div class="new-in-sale tab-content relises-active">
					<ul class="menu">
						<?php 
							$args = array(
							    'numberposts'	=>	$instance['count'],
							    'orderby'		=>	'date',
							    'order'			=>	'DESC',
							    'post_type'		=>	'post',
							    'post_status'	=>	'publish',
								'meta_key'		=>	'game_relise_new',
								'meta_value'	=>	'on',
							);
							$posts_array = get_posts( $args );
							foreach ($posts_array as $post): ?>
								<li><a href="<?php echo get_permalink($post->ID); ?>"><?php $_ = get_post_meta($post->ID,'game_relise_name'); if(!empty($_[0])) echo $_[0]; else echo $post->post_title; ?></a> <p><?php $_ = get_post_meta($post->ID,'game_relise_date'); echo $_[0]; ?></p><span></span></li>
								<?php 
							endforeach;
							wp_reset_query();
						?>
					</ul>
				</div>
				<div class="future tab-content">
					<ul class="menu">
						<?php 
							$args = array(
							    'numberposts'	=>	$instance['count'],
							    'orderby'		=>	'date',
							    'order'			=>	'DESC',
							    'post_type'		=>	'post',
							    'post_status'	=>	'publish',
								'meta_key'		=>	'game_relise_will',
								'meta_value'	=>	'on',
							);
							$posts_array = get_posts( $args );
							foreach ($posts_array as $post): ?>
								<li><a href="<?php echo get_permalink($post->ID); ?>"><?php $_ = get_post_meta($post->ID,'game_relise_name'); if(!empty($_[0])) echo $_[0]; else echo $post->post_title; ?></a> <p><?php $_ = get_post_meta($post->ID,'game_relise_date'); echo $_[0]; ?></p><span></span></li>
								<?php 
							endforeach;
							wp_reset_query();
						?>
					</ul>
				</div>
			</div>
		</div>
		<?php
		echo $after_widget;
	}
}